package com.cmc.cloud.cmclink.doc.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoRejectReasonErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SpecialCargoRejectReasonConvert;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoRejectReasonDO;
import com.cmc.cloud.cmclink.doc.mapper.SpecialCargoRejectReasonMapper;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoRejectReasonService;
import com.cmc.cloud.cmclink.doc.vo.special.rejectreasonvo.SpecialCargoRejectReasonCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.rejectreasonvo.SpecialCargoRejectReasonPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.rejectreasonvo.SpecialCargoRejectReasonUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 特种货审批长常用拒绝理由 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class SpecialCargoRejectReasonServiceImpl implements SpecialCargoRejectReasonService {

    @Resource
    private SpecialCargoRejectReasonMapper specialCargoRejectReasonMapper;

    @Override
    public void createSpecialCargoRejectReason(SpecialCargoRejectReasonCreateReqVO createReqVO) {
        // 插入
        SpecialCargoRejectReasonDO specialCargoRejectReason =
            SpecialCargoRejectReasonConvert.INSTANCE.convert(createReqVO);
        specialCargoRejectReasonMapper.insert(specialCargoRejectReason);
    }

    @Override
    public void updateSpecialCargoRejectReason(SpecialCargoRejectReasonUpdateReqVO updateReqVO) {
        // 校验存在
        validateSpecialCargoRejectReasonExists(updateReqVO.getId());
        // 更新
        SpecialCargoRejectReasonDO updateObj = SpecialCargoRejectReasonConvert.INSTANCE.convert(updateReqVO);
        specialCargoRejectReasonMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialCargoRejectReason(Long id) {
        // 校验存在
        validateSpecialCargoRejectReasonExists(id);
        // 删除
        specialCargoRejectReasonMapper.deleteById(id);
    }

    private void validateSpecialCargoRejectReasonExists(Long id) {
        if (specialCargoRejectReasonMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SpecialCargoRejectReasonErrorCodeConstants.REJECT_REASON_EXISTED);
        }
    }

    @Override
    public SpecialCargoRejectReasonDO getSpecialCargoRejectReason(Long id) {
        return specialCargoRejectReasonMapper.selectById(id);
    }

    @Override
    public PageResult<SpecialCargoRejectReasonDO>
        getSpecialCargoRejectReasonPage(SpecialCargoRejectReasonPageReqVO reqVO) {
        LambdaQueryWrapperX<SpecialCargoRejectReasonDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(SpecialCargoRejectReasonDO::getReasonType, reqVO.getReasonType())
            .orderByAsc(SpecialCargoRejectReasonDO::getReasonType).orderByAsc(SpecialCargoRejectReasonDO::getSortNo);
        return specialCargoRejectReasonMapper.selectPage(reqVO, queryWrapperX);
    }
}
